package com.sgb.kjwl.viewmodel.enterprise;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.EnterpriseAPIManager;
import com.sgb.kjwl.model.entity.enterprise.TeamIntelligenceEntity;
import com.sgb.kjwl.model.entity.enterprise.UserCenterTeamIntelligenceEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.view.ui.activity.enterprise.TeamIntelligenceListActivity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class TeamIntelligenceListModel{
    private TeamIntelligenceListActivity activity;
    int pageNum = 1;

    public TeamIntelligenceListModel(TeamIntelligenceListActivity activity) {
        this.activity = activity;
    }

    /**
     *
     * @param isLodMore
     * @param teamNo
     */
    public void getTeamIntelligenceList(boolean isLodMore, String teamNo) {
        if (!isLodMore){
            activity.loading();
        }
        pageNum = isLodMore ? pageNum + 1 : 1;
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getQualification(teamNo, pageNum, 20).enqueue(new Callback<BaseEntity<ListResponse<List<TeamIntelligenceEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<TeamIntelligenceEntity>>>> call, Response<BaseEntity<ListResponse<List<TeamIntelligenceEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    activity.showList(new ArrayList<>(), isLodMore);
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showList(response.body().getData().getList(), isLodMore);
                    activity.success();
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                    activity.showList(new ArrayList<>(), isLodMore);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<TeamIntelligenceEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                activity.showList(new ArrayList<>(), isLodMore);
            }
        });
    }

    /**
     *
     * @param isLodMore
     */
    public void getTeamIntelligenceListUserCenter(boolean isLodMore) {
        if (!isLodMore){
            activity.loading();
        }
        pageNum = isLodMore ? pageNum + 1 : 1;
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getQualificationUserCenter(pageNum, 20).enqueue(new Callback<BaseEntity<ListResponse<List<UserCenterTeamIntelligenceEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<UserCenterTeamIntelligenceEntity>>>> call, Response<BaseEntity<ListResponse<List<UserCenterTeamIntelligenceEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    activity.showUserList(new ArrayList<>(), isLodMore);
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showUserList(response.body().getData().getList(), isLodMore);
                    activity.success();
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                    activity.showList(new ArrayList<>(), isLodMore);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<UserCenterTeamIntelligenceEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                activity.showList(new ArrayList<>(), isLodMore);
            }
        });
    }
}
